package bookworm;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.LinkedList;



public class GetReviewerRatingFromAPI implements GetReviewersRatingsInterface
{
	public LinkedList<Integer> getAllReviewersRatings(LinkedList<String> reviewersNames)
	{
		LinkedList<Integer> allReviewersRatings = new LinkedList<Integer>();
		
		for(int i = 0; i < reviewersNames.size(); i++)
		{
			String reviewerName = reviewersNames.get(i);
			int reviewerRating = this.getReviewerRating(reviewerName);
			allReviewersRatings.add(reviewerRating);
		}
		
		/*
		 let's write the results(found using a connection) in a text file so that we can use these results
		 later without a connection...
		*/
		
		
		return allReviewersRatings;
	}
	
	public int getReviewerRating(String reviewerName)
	{
		//the reviewer's rating won't be calculated here: this is a job for *drums roll* CalculatesRatingsBookworm!
		int reviewerRating = CalculatesRatingsBookworm.calculateReviewerRating(reviewerName);
		
		//let's write the reviewer's rating in a text file so that we can get this rating without internet later
		this.writeReviewerRatingToTextFile(reviewerName, reviewerRating);
		
		return reviewerRating;
	}
	
	
	
	private void writeReviewerRatingToTextFile(String reviewerName, int reviewerRating)
	{
		try
		{	
			FileOutputStream fileOutputStream = new FileOutputStream("texts/reviewerratings.txt", true);
			OutputStreamWriter outputStreamWriter =
					new OutputStreamWriter(fileOutputStream, "UTF8");
			
			
				
			String reviewerRatingToWrite = reviewerName + "==" + reviewerRating + "\r\n";
			outputStreamWriter.write(reviewerRatingToWrite);
				
			
			outputStreamWriter.close();
			fileOutputStream.close();
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
	}
	
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
}

